package tcpOptimized;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ServerImpl implements Runnable{
    private Socket socket;

    public ServerImpl(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try(InputStream is = socket.getInputStream();
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("after_buy\\"+ UUID.randomUUID()+".jpg"));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));) {

            byte[] bytes = new byte[1024];
            int len;
            while ((len = is.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            bos.close();
            //告知客户端上传成功
            // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("上传成功啦");
            bw.newLine();
            bw.flush();
            //给客户端写一个结束标记
            socket.shutdownOutput();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
